import { SysUser } from '../db/models/sys_user'

export class UserManager {
  private static instance: UserManager
  // private _sysUser: Array<SysUser>
  // private readonly _id: string

  constructor() {
    // this._sysUser = []
    // this._id = id
    // this._loadFromDB()
  }

  // private async _loadFromDB() {
  //   const userModule = await SysUser.findAll({})
  //   this.setUser(userModule)
  // }

  public static getInstance(): UserManager {
    if (!UserManager.instance) {
      UserManager.instance = new UserManager()
    }
    return UserManager.instance
  }

  // setUser(menus: Array<SysUser>) {
  //   this._sysUser = menus
  // }

  //根据参数修改数据
  async update(id: number, userName: string, userPass: string, userSort: string) {
    return await SysUser.update(
      {
        userName: userName,
        userPass: userPass,
        userSort: userSort
      },
      {
        where: {
          id: id
        }
      }
    )
  }

  //根据参数添加数据
  async addUser(userName: string, userPass: string, userSort: string) {
    return await SysUser.create({
      userName: userName,
      userPass: userPass,
      userSort: userSort
    })
  }

  //根据id删除数据
  async removeUser(dId: string) {
    await SysUser.destroy({
      where: {
        id: dId
      }
    }).then((value: number) => {
      if (value > 0) {
        // this._loadFromDB()
      }
    })
  }

  //查询全部数据
  async getAll() {
    return await SysUser.findAll({}).then((value: Array<SysUser>) => {
      return JSON.stringify(value)
    })
  }

  //根据用户名称查询数据
  async getByName(name: string) {
    return await SysUser.findAll({
      where: {
        userName: name
      }
    }).then((value: Array<SysUser>) => {
      return JSON.stringify(value)
    })
  }
}
